package com.ruoyi.contractAndAudit.controller;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessCode;
import com.ruoyi.contractAndAudit.domain.DocContract1;
import com.ruoyi.contractAndAudit.mapper.vo.SignContractVO;
import com.ruoyi.contractAndAudit.service.IDocAuditService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ruoyi.common.utils.SecurityUtils;

import java.util.Date;

@RestController
@Api(tags = "签约模块")
@RequestMapping("/contractAndAudit/docAudit")
public class DocAuditController extends BaseController {

    @Autowired
    private IDocAuditService docAuditService;

    /**
     * 用户点击签约按钮生成签约表
     * service层再根据用户id和团队id搜出当前订单id再写回用户表
     *
     * @param signContractVO 签约合同信息传输对象，包含团队ID和合同期限等信息
     */
    @ApiOperation("用户点击签约按钮生成签约表")
    @PostMapping("/addContract")
    public void addContract(@RequestBody SignContractVO signContractVO) {
        // 获取当前登录用户ID
        Long userId = SecurityUtils.getUserId();
        // 使用 hutool 工具类生成雪花算法唯一订单 ID
        long orderId = IdUtil.getSnowflake(1, 1).nextId();

        // 构建对象并设置相关属性
        DocContract1 docContract1 = new DocContract1();
        docContract1.setUserId(userId);
        docContract1.setTeamId(signContractVO.getTeamId());
        docContract1.setContractTime(new Date());
        docContract1.setContractDuration(signContractVO.getContractDuration());
        docContract1.setContractState(0); // 签约状态：0-待签约 1-已签约
        docContract1.setOrderId(orderId);
        docContract1.setAuditState(0);    // 审核状态：0-待审核 1-审核已通过 2-审核未通过

        docAuditService.addContract(docContract1);
    }

    /**
     * 获取待签约团队数据
     */
//    @ApiOperation("获取待签约团队数据")
//    @GetMapping("/getDocContract")
//    public AjaxResult getDocContract() {
//        // 获取当前登录用户ID
//        Long userId = SecurityUtils.getUserId();
//        return AjaxResult.success(docAuditService.getDocContract(userId));
//    }

    /**
     * 获取待签约团队数据（支持分页）
     */
    @ApiOperation("获取待签约团队数据")
    @GetMapping("/getDocContract")
    public TableDataInfo getDocContract() {
        // 获取当前登录用户ID
        Long userId = SecurityUtils.getUserId();
        startPage();
        return getDataTable(docAuditService.getDocContract(userId));
    }

    /**
     * 提交签约校验（已付款和审核未通过不管，其他的显示：请勿重复提交签约订单）
     *
     * @param teamId 团队ID
     * @return AjaxResult 状态码
     */
    @ApiOperation("提交签约校验")
    @PostMapping("/checkContract/{team_id}")
    public AjaxResult checkContract(@PathVariable("team_id") Long teamId) {
        // 获取当前登录用户ID
        Long userId = SecurityUtils.getUserId();
        int checkContracted = docAuditService.checkContracted(userId, teamId);
        int checkContracting = docAuditService.checkContracting(userId, teamId);

        if (checkContracted != 0) { // 枚举 Jackson 默认序列化成枚举名字符串
            return AjaxResult.success(BusinessCode.CHECK_CONTRACTED);
        } else if (checkContracting != 0) {
            return AjaxResult.success(BusinessCode.CHECK_CONTRACTING);
        }
        return AjaxResult.success();
    }

}
